/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GANTTSCENE_H
#define GANTTSCENE_H

#include <QGraphicsLineItem>
#include <QGraphicsScene>

#include <procycle/cycle.h>

#include "globals.h"
#include "pcchild.h"


class GanttAxis;
class GanttTask;
class GanttMTask;
class GanttCycle;
class GanttLink;


static const QString DEFAULT_ATASK_COLOR = "#7f7fff";
static const QString DEFAULT_MTASK_COLOR = "#5a5a60";

class GanttScene : public QGraphicsScene, public PCChild
{
	Q_OBJECT
public:


	GanttScene(ProCycle* app, QRectF rect, QObject *parent = 0);
	virtual ~GanttScene() {
	}

	static GanttTask* createGTask(ProCycle* app, PC::Task* task, GanttCycle* gcycle, GanttMTask* parent=0);

	PC::Cycle* cycle() {
		return m_cycle;
	}

	void resetCycle(PC::Cycle* cycle);

	GanttCycle*	ganttCycle() {
		return m_ganttCycle;
	}

	GanttAxis* axis() {
		return m_axis;
	}

	GanttTask* resolvTask(QString tbsId);

	GanttTask* taskAt(const QPointF& p);


	void editModeChanged();

	void initSizePolicy(const QSize& size);

	static QString colorStrForTask(PC::Task* task);
	static QColor colorForTask(PC::Task* task);
	static QColor borderColor(const QColor& back);

public slots:
	void processViewResize(QSize newSize);
	void processExpansionChange(PC::Task* task);

	void processTaskTimeChanged(PC::Task*);
	void processTaskValueChanged(PC::Task* task, PC::Task::Data data);

	void processTaskCreated(PC::MacroTask* parent, PC::Task* task);
	void processTaskWillDelete(PC::Task* task);
	void processTaskTransformed(PC::MacroTask* parent, int taskPos);
	void processTaskMoved(PC::MacroTask* parentTo, PC::Task* parentFrom, int posTo, int posFrom);

	void processLinkCreated(PC::Link* link);
	void processLinkWillDelete(PC::Link* link);

	void processScheduled();

	void selectTask(PC::Task* selected, PC::Task* unselected);
	void selectLink(PC::Link* selected, PC::Link* unselected);

	void toggleCriticalPath(bool on);
	void toggleCycleOverlap(bool on);
	void toggleTasksFloat(bool on);

	void zoomFit();
	void zoomIn();
	void zoomOut();
	void toggleZoomWindow(bool on);

private:
	PC::Cycle*			m_cycle;

	GanttAxis*			m_axis;
	GanttCycle*			m_ganttCycle;

	QGraphicsLineItem*	m_cycleTime;

	int					m_preferredWidth;
};

#endif // GANTTSCENE_H
